package com.zr.command.service.impl;
import java.util.List;
import com.zr.common.core.domain.entity.SysUser;
import com.zr.common.utils.DateUtils;
import com.zr.common.utils.SecurityUtils;
import com.zr.eim.domain.EimDataPerson;
import com.zr.eim.domain.EimOuterCitizen;
import com.zr.eim.mapper.EimDataPersonMapper;
import com.zr.eim.mapper.EimOuterCitizenMapper;
import com.zr.system.mapper.SysOrganizationMapper;
import com.zr.task.domain.EimTaskLog;
import com.zr.task.mapper.EimTaskLogMapper;
import org.springframework.stereotype.Service;
import com.zr.command.mapper.EimCommandScheduleMapper;
import com.zr.command.domain.EimCommandSchedule;
import com.zr.command.service.IEimCommandScheduleService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/**
 * 指挥调度工单Service业务层处理
 * 
 * @author chengkai
 * @date 2022-10-21
 */
@Service
public class EimCommandScheduleServiceImpl implements IEimCommandScheduleService 
{
    @Resource
    private EimCommandScheduleMapper eimCommandScheduleMapper;

    @Resource
    private EimDataPersonMapper eimDataPersonMapper;

    @Resource
    private EimTaskLogMapper eimTaskLogMapper;

    @Resource
    private SysOrganizationMapper sysOrganizationMapper;

    @Resource
    private EimOuterCitizenMapper eimOuterCitizenMapper;

    /**
     * 查询指挥调度工单
     * @param id 指挥调度工单主键
     * @return 指挥调度工单
     */
    @Override
    public EimCommandSchedule selectEimCommandScheduleById(Long id)
    {
        return eimCommandScheduleMapper.selectEimCommandScheduleById(id);
    }

    /**
     * 查询指挥调度工单列表
     * @param eimCommandSchedule 指挥调度工单
     * @return 指挥调度工单
     */
    @Override
    public List<EimCommandSchedule> selectEimCommandScheduleList(EimCommandSchedule eimCommandSchedule)
    {
        return eimCommandScheduleMapper.selectEimCommandScheduleList(eimCommandSchedule);
    }

    /**
     * 新增指挥调度工单
     * @param eimCommandSchedule 指挥调度工单
     */
    @Transactional
    @Override
    public int insertEimCommandSchedule(EimCommandSchedule eimCommandSchedule)
    {
        eimCommandSchedule.setCreateTime(DateUtils.getNowDate());
        if (eimCommandSchedule.getGroupId()==null){
            eimCommandSchedule.setStatus("0");
        }else {
            eimCommandSchedule.setStatus("1");
        }
        EimDataPerson person=eimCommandSchedule.getEimDataPerson();
        String administratorId = sysOrganizationMapper.selectAdministratorIdByCode(person.getAddressCode());
        if(person!=null && person.getIfDiagnosis()==1 && person.getControlStatus()==0){
            eimDataPersonMapper.updateEimDataPersonControlStatus(eimCommandSchedule.getIdCard(),2);
        }else if(person!=null && person.getIfCloseContact()==1 && person.getControlStatus()==0){
            eimDataPersonMapper.updateEimDataPersonControlStatus(eimCommandSchedule.getIdCard(),1);
        }else if(person!=null && person.getIfDiagnosis()==1 && person.getControlStatus()!=0 && person.getIfSampleSanitize()==0){
            eimDataPersonMapper.updateEimDataPersonSampleSanitize(eimCommandSchedule.getIdCard(),1);
        }
        int row=eimCommandScheduleMapper.insertEimCommandSchedule(eimCommandSchedule);
        EimTaskLog log=new EimTaskLog();
        log.setTaskId(eimCommandSchedule.getId());
        log.setTaskType(eimCommandSchedule.getType());
        log.setTaskStatus(eimCommandSchedule.getStatus());
        log.setTaskUserId(administratorId);
        log.setCreateTime(DateUtils.getNowDate());
        if(eimCommandSchedule.getGroupId()!=null){log.setTaskPerform("分配了");}
        eimTaskLogMapper.insertEimTaskLog(log);
        return row;
    }

    /**
     * 修改指挥调度工单
     * @param eimCommandSchedule 指挥调度工单
     */
    @Override
    public int updateEimCommandSchedule(EimCommandSchedule eimCommandSchedule)
    {
        EimTaskLog log=new EimTaskLog();
        log.setTaskId(eimCommandSchedule.getId());
        log.setTaskType(eimCommandSchedule.getType());
        if(eimCommandSchedule.getGroupId()!=null){
            eimCommandSchedule.setStatus("1");
            log.setTaskStatus("1");
            log.setTaskPerform("分配了");
        }
        eimTaskLogMapper.updateEimTaskLogByTaskId(log);
        return eimCommandScheduleMapper.updateEimCommandSchedule(eimCommandSchedule);
    }

    /**
     * 删除指挥调度工单信息
     * @param eimCommandSchedule 指挥调度工单
     * @return 删除行数
     */
    @Transactional
    @Override
    public int deleteEimCommandScheduleById(EimCommandSchedule eimCommandSchedule)
    {

        EimDataPerson person=eimCommandSchedule.getEimDataPerson();
        if(person.getIfSampleSanitize()==1){
            //将采样消杀状态改为未采样
            eimDataPersonMapper.updateEimDataPersonSampleSanitize(eimCommandSchedule.getIdCard(),0);
        }else {
            //将人员状态更改为未管控状态
            eimDataPersonMapper.updateEimDataPersonControlStatus(eimCommandSchedule.getIdCard(),0);
        }
        //删除任务日志
        eimTaskLogMapper.deleteEimTaskLogByTaskId(eimCommandSchedule.getId());
        //删除未分配指挥工单
        return eimCommandScheduleMapper.deleteEimCommandScheduleById(eimCommandSchedule.getId());
    }

    /**
     * 查询指挥调度工单列表
     */
    @Override
    public List<EimCommandSchedule> listEimCommandSchedule(EimCommandSchedule eimCommandSchedule,SysUser user) {
        return eimCommandScheduleMapper.listEimCommandSchedule(eimCommandSchedule,user);
    }

    /**
     * 查询隔离点列表
     */
    @Override
    public List<String> listQuarantineAddress(Long organizationId) {
        return eimCommandScheduleMapper.listQuarantineAddress(organizationId);
    }

    /**
     * 分配指挥调度单给负责小组
     * @param  ids 指挥调度工单ID列表
     * @param groupId 负责小组ID
     */
    @Transactional
    @Override
    public int dispatchScheduleById(List<Long> ids, Long groupId) {
        int count=0;
        for(Long id:ids){
            //更新任务日志动作
            EimTaskLog log=new EimTaskLog();
            log.setTaskId(id);
            log.setTaskStatus("1");
            log.setTaskPerform("分配了");
            eimTaskLogMapper.updateEimTaskLogByTaskId(log);
            //更新调度工单负责小组
            eimCommandScheduleMapper.dispatchScheduleById(id, groupId);
            count++;
        }
        return count;
    }

    /**
     * 根据流调结果新增指挥调度工单
     * @param personList 流调出来的确诊或者密接人员列表
     */
    @Transactional
    @Override
    public int insertEimCommandSchedulesByCovidFlow(List<EimDataPerson> personList){
        Long addressCode=sysOrganizationMapper.selectAddressCode(SecurityUtils.getLoginUser().getUser().getUserName());
        int count=0;
        for (EimDataPerson person : personList) {
            if (person.getAddressCode()==addressCode){
                EimCommandSchedule schedule = new EimCommandSchedule();
                EimCommandSchedule schedule1 = null;//如果是确诊病例，需要新增采样消杀单
                if (person.getIfDiagnosis() == 1L) {
                    schedule.setTitle("120转运工单");
                    schedule.setType("3");
                    schedule.setDescription("请尽快完成确诊病例转运");
                    schedule1 = new EimCommandSchedule();
                    schedule1.setTitle("采样消杀工单");
                    schedule1.setType("1");
                    schedule1.setDescription("请尽快对病例所在区域完成采样消杀工作");
                    schedule1.setIdCard(person.getPersonId());
                    schedule1.setEimDataPerson(person);
                } else if (person.getIfCloseContact() == 1L) {
                    schedule.setTitle("核酸检测工单");
                    schedule.setType("2");
                    schedule.setDescription("请尽快对确诊病例完成核酸检测");
                }
                schedule.setIdCard(person.getPersonId());
                schedule.setEimDataPerson(person);
                insertEimCommandSchedule(schedule);
                if (schedule1 != null) {
                    insertEimCommandSchedule(schedule1);
                }
                count++;
            }
        }
        return count;
    }


    /**
     * 查询指挥调度工单各种状态的数量
     */
    @Override
    public int selectScheduleCountByStatus(Integer status, String  userName) {
        return eimCommandScheduleMapper.selectScheduleCountByStatus(status,userName);
    }

    /**
     * 查询指挥调度工单各种类型的数量
     */
    @Override
    public int selectScheduleCountByType(Integer type, String userName) {
        return eimCommandScheduleMapper.selectScheduleCountByType(type,userName);
    }

    /**
     * 查询状态未未分配的指挥调度工单列表
     */
    @Override
    public List<EimCommandSchedule> listUndispatchedEimCommandSchedule(SysUser user) {
        return eimCommandScheduleMapper.listUndispatchedEimCommandSchedule(user);
    }

    /**
     * 对外部输入体温异常或来自高风险区域的人员生成120转运工单
     */
    @Override
    public int insertEimCommandScheduleByOuterCitizen(EimOuterCitizen citizen) {
        EimCommandSchedule schedule = new EimCommandSchedule();
        schedule.setTitle("120转运工单");
        schedule.setType("3");
        schedule.setDescription("请尽快完成外部输入异常人员转运");
        schedule.setIdCard(citizen.getIdCard());
        schedule.setCreateTime(DateUtils.getNowDate());
        schedule.setStatus("0");
        int row=eimCommandScheduleMapper.insertEimCommandSchedule(schedule);
        EimTaskLog log=new EimTaskLog();
        log.setTaskId(schedule.getId());
        log.setTaskType(schedule.getType());
        log.setTaskStatus("0");
        if(citizen.getInputPosition().contains("长春西站")){
            log.setTaskUserId("jkm");
        }else if(citizen.getInputPosition().contains("龙嘉机场")){
            log.setTaskUserId("admin10");
        }else {
            log.setTaskUserId("ldh");
        }
        eimOuterCitizenMapper.updateEimOuterCitizenControlStatus(citizen.getIdCard(),2);
        log.setCreateTime(DateUtils.getNowDate());
        eimTaskLogMapper.insertEimTaskLog(log);
        return row;
    }

    /**
     * 修改指挥调度工单
     * 需求大厅：APP
     */
    @Override
    public int editEimCommandSchedule(EimCommandSchedule eimCommandSchedule) {
        return eimCommandScheduleMapper.editEimCommandSchedule(eimCommandSchedule);
    }

}
